﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Configuration;
using Midapex.SmartOrm.Tools.Wizard.Properties;


namespace Midapex.SmartOrm.Tools.Wizard
{
    public partial class DataSourceControl : UserControl
    {
        public DataSourceControl()
        {
            InitializeComponent();
            sqlServerForm = new SqlServerConnForm();
            mySqlForm = new MySqlConnForm();
            accessForm = new AccessConnForm();
            sqliteForm = new SQLiteConnForm();
        }

        SqlServerConnForm sqlServerForm;
        MySqlConnForm mySqlForm;
        AccessConnForm accessForm;
        SQLiteConnForm sqliteForm;

        public DataSourceType DataBaseType
        {
            get { return (DataSourceType)comboBoxDataSourceType.SelectedIndex; }
        }

        public string DbConnectionString
        {
            get { return this.tbConnString.Text; }
        }

        int oldIndex = -1;

        private void comboBoxDataSourceType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (init)
                return;

            linkLabelCreateDbConn_LinkClicked(this, null);
            oldIndex = comboBoxDataSourceType.SelectedIndex;
        }

        private void DataSourceControl_Load(object sender, EventArgs e)
        {
            comboBoxDataSourceType.SelectedIndex = -1;  
        }

        private void linkLabelReadconfig_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            ReadAppConfig();
        }

        private void linkLabelCreateDbConn_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            switch (this.comboBoxDataSourceType.SelectedIndex)
            {
                case 0:
                    if (DialogResult.OK == sqlServerForm.ShowDialog())
                    {
                        this.tbConnString.Text = sqlServerForm.GetConnString();
                    }
                    else
                    {
                        SetOldType();
                    }
                    break;
                case 1:
                    if (DialogResult.OK == accessForm.ShowDialog())
                    {
                        this.tbConnString.Text = accessForm.GetConnString();
                    }
                    else
                        SetOldType();
                    break;
                case 2:
                    if (DialogResult.OK == mySqlForm.ShowDialog())
                    {
                        this.tbConnString.Text = mySqlForm.GetConnString();
                    }
                    else
                        SetOldType();
                    break;
                case 4:
                    if (DialogResult.OK == sqliteForm.ShowDialog())
                    {
                        this.tbConnString.Text = sqliteForm.GetConnString();
                    }
                    else
                        SetOldType();
                    break;

                default:
                    MessageBox.Show(this, "很遗憾，没有实现的数据库连接创建方式，我们会在以后的版本完善。");
                    SetOldType();
                    break;
            }
        }

        private void SetOldType()
        {
            init = true;
            comboBoxDataSourceType.SelectedIndex = oldIndex;
            init = false;
        }

        private void buttonWriteAppConfig_Click(object sender, EventArgs e)
        {
            WriteAppConfig();
            MessageBox.Show(this, "保存设置成功。");
        }

        bool init = true;

        internal void WriteAppConfig()
        {
            OrmConfig config = new OrmConfig();
            config.DbConnectionString = this.DbConnectionString;
            config.DbType = this.DataBaseType;

            Configuration configuration = OrmWizard.Configuration;

            configuration.Sections.Remove(OrmConfig.SectionName);
            configuration.Sections.Add(OrmConfig.SectionName, config);

            configuration.Save();

            OrmSessionsFactory.SetConfig(DataBaseType, this.tbConnString.Text);
        }

        internal void ReadAppConfig()
        {
            init = true;
          
            Configuration config = OrmWizard.Configuration;
            if(config==null)
            {
                init = false;
                throw new Exception(Resources.SelectAppFirst);
            }

            this.appConfigFile.Text = config.FilePath;

            OrmConfig ormConfig = config.GetSection(OrmConfig.SectionName) as OrmConfig;
            if (ormConfig == null)
            {
                init = false;
                return;    
            }

            try
            {
                oldIndex = comboBoxDataSourceType.SelectedIndex = (int)ormConfig.DbType;
                tbConnString.Text = ormConfig.DbConnectionString;
            }
            catch(Exception)
            {
            }
           
            init = false;
        }
    }
}
